package org.airline.action;

import java.util.List;

import javax.annotation.Resource;

import org.airline.common.IModuleName;
import org.airline.entity.Message;
import org.airline.entity.PageBean;
import org.airline.service.MessageService;
import org.springframework.stereotype.Component;

@Component("MessageAction")
@SuppressWarnings("all")
public class MessageAction extends SuperAction {

	private int page = 1;
	private int pageSize = 5;
	private String forwardURL;
	private IModuleName iModuleName;
	private String keyWord;
	private Message message;
	private MessageService messageService;
	private PageBean pageBean;
	
	
	// 添加留言
	public String addMessageInfo() throws Exception {
		try {
			messageService.saveMessage(message);
		} catch (Exception e) {
			logFailureString(iModuleName.ADD, iModuleName.ADD, "添加留言失败", e) ;
			e.printStackTrace();
		}
		logSuccessString(iModuleName.ADD, iModuleName.ADD, "添加留言成功");
		forwardURL = "MessageInfo!getMessageInfo?page=1";
		return "message";
	}

	// 前台获取所有留言并分页显示
	public String getMessageInfo() throws Exception {
		try {
			pageBean = messageService.queryForPage(pageSize, page);
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "获取所有留言失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "获取所有留言成功");
		forwardURL = "/jsp/showMessage_test.jsp";
		return SUCCESS;
	}
	// 后台获取所有留言并分页显示
	public String getAllMessageInfo() throws Exception {
		try {
			pageBean = messageService.queryForPage(pageSize, page);
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "获取所有留言失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "获取所有留言成功");
		forwardURL = "/admin/showAllMessage.jsp";
		return SUCCESS;
	}
	
	// 根据用户ID查询该用户的所有留言
	public String getMessageInfoByUserId() throws Exception {
		try {
			pageBean = messageService.queryForPageByUserId(message.getUser().getId(), pageSize, page);
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "根据用户ID查询留言失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "根据用户ID查询留言成功");
		forwardURL = "/jsp/showMessage.jsp";
		return SUCCESS;
	}
	
	// 后台根据ID查询留言信息
	public String getMessageInfoById() throws Exception {
		try {
			message = messageService.getMessageById(message.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "根据ID查询留言信息失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "根据ID查询留言信息成功");
		forwardURL = "/admin/updateMessage.jsp";
		return SUCCESS;
	}
	// 前台根据ID查询留言信息
	public String getMessageById() throws Exception {
		try {
			message = messageService.getMessageById(message.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.GET, iModuleName.GET, "根据ID查询留言信息失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.GET, iModuleName.GET, "根据ID查询留言信息成功");
		forwardURL = "/jsp/updateMessage.jsp";
		return SUCCESS;
	}
	
	// 前台修改留言信息
	public String updateMessage() throws Exception {
		try {
			messageService.updateMessage(message);
		} catch (Exception e) {
			logFailureString(iModuleName.UPDATE, iModuleName.UPDATE, "修改留言信息失败", e);
			e.printStackTrace();
		}
		forwardURL = "/jsp/showMessage.jsp";
		return SUCCESS;
	}
	
	// 更新留言信息
	public String updateMessageInfo() throws Exception {
		try {
			messageService.updateMessage(message);
		} catch (Exception e) {
			logFailureString(iModuleName.UPDATE, iModuleName.UPDATE, "更新留言信息失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.UPDATE, iModuleName.UPDATE, "更新留言成功");
		forwardURL = "MessageInfo!getAllMessageInfo?page=1";
		return "message";
	}
	
	// 删除留言信息
	public String deleteMessageInfo() throws Exception {
		try {
			messageService.deleteMessage(message.getId());
		} catch (Exception e) {
			logFailureString(iModuleName.DELETE, iModuleName.DELETE, "删除留言信息失败", e);
			e.printStackTrace();
		}
		logSuccessString(iModuleName.DELETE, iModuleName.DELETE, "删除留言信息成功");
		forwardURL = "MessageInfo!getAllMessageInfo?page=1";
		return "message";
	}
	
	// 根据关键字分页查询用户留言信息
	public String getMessagesByKeyWords() throws Exception {
		try {
			pageBean = messageService.queryForPageByKeyWords(keyWord, pageSize,
					page);
		} catch (Exception e) {
			super.logFailureString(iModuleName.GET, iModuleName.GET,
					"根据关键字分页查询留言信息失败", e);
			e.printStackTrace();
		}
		super.logSuccessString(iModuleName.GET, iModuleName.GET,
				"根据关键字分页查询留言信息成功");
		forwardURL = "/admin/showAllMessage.jsp";
		return SUCCESS;
	}
	
	public String getForwardURL() {
		return forwardURL;
	}

	public void setForwardURL(String forwardURL) {
		this.forwardURL = forwardURL;
	}

	public IModuleName getiModuleName() {
		return iModuleName;
	}

	public void setiModuleName(IModuleName iModuleName) {
		this.iModuleName = iModuleName;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public MessageService getMessageService() {
		return messageService;
	}

	@Resource(name = "MessageService")
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
	
}
